/* eslint-disable brace-style,prefer-const,one-var */
// 验证规则
export const validator = {
  normal: (rule, value, callback) => {
    if (!value && value !== 0) {
      return callback(new Error(rule.name + '不能为空!'))
    } else {
      callback()
    }
  },
  numberBiggerThan0: (rule, value, callback) => {
    const regex = new RegExp(/^[1-9]\d*$/)
    if (regex.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的' + rule.name))
    }
  },
  phone: (rule, value, callback) => {
    if (!value) {
      return callback(new Error(rule.name + '不能为空!'))
    }
    const regex = new RegExp(/^1\d{10}$/)
    if (regex.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的' + rule.name))
    }
  },

  phone2: (rule, value, callback) => {
    if (!value) {
      return callback()
    }
    const regex = new RegExp(/^1\d{10}$/)
    if (regex.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的' + rule.name))
    }
  },
  email: (rule, value, callback) => {
    if (!value) {
      return callback(new Error(rule.name + '不能为空!'))
    }
    const regex = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)
    if (regex.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的' + rule.name))
    }
  },
  url: (rule, value, callback) => {
    if (!value && value !== 0) {
      return callback(new Error(rule.name + '不能为空!'))
    }
    const regex = new RegExp(/(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/)
    if (regex.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的' + rule.name))
    }
  },
  number: (rule, value, callback) => {
    if (!value && value !== 0) {
      return callback(new Error(rule.name + '不能为空!'))
    }
    console.log(value, isNaN(value))
    if (isNaN(value)) {
      return callback(new Error('请输入数值'))
    } else {
      callback()
    }
  },
  // 选填的数字类型验证
  xtnumber: (rule, value, callback) => {
    if (value) {
      if (isNaN(value)) {
        return callback(new Error('请输入数值'))
      } else {
        callback()
      }
    } else {
      callback()
    }
  },
  // 选填的两位小数校验
  price: (rule, value, callback) => {
    let reg = /^(0|[1-9]\d*)(\.\d{1,2})?$/g
    value = '' + value
    if (value != null && !value.match(reg)) {
      return callback(new Error('请输入最多两位小数'))
    } else {
      callback()
    }
  },
  // 选填的地址
  address: (rule, value, callback) => {
    if (value && value.length !== 3) {
      return callback(new Error('请选择正确的省市区'))
    } else {
      callback()
    }
  },
  age: (rule, value, callback) => {
    if (!value && value !== 0) {
      return callback(new Error(rule.name + '不能为空!'))
    }
    if (isNaN(value)) {
      return callback(new Error('请输入数值!'))
    } else if (value < 20 || value > 80) {
      return callback(new Error('请输入20以上80以下的数值!'))
    } else {
      callback()
    }
  },
  is: (rule, value, callback) => {
    if (value === '' || value === null) {
      return callback(new Error(rule.name + '不能为空!'))
    } else {
      callback()
    }
  },
  date: (rule, value, callback) => {
    if (!value) {
      return callback(new Error('请输入日期'))
    }
    const regex = new RegExp(/^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/)
    if (regex.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的日期格式'))
    }
  },

  homeCard: (rule, value, callback) => {
    if (!value && value !== 0) {
      return callback(new Error(rule.name + '不能为空!'))
    }
    var re1 = new RegExp(/^[a-zA-Z]\d{8}$/)
    if (re1.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的' + rule.name))
    }
  },
  /**
   * 身份证
   * https://www.jb51.net/article/73496.htm
   */
  identity: (rule, value, callback) => {
    if (!value && value !== 0) {
      return callback(new Error(rule.name + '不能为空!'))
    }
    // let regex = new RegExp(/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/);
    const isRight = validateIdCard(value)
    if (isRight) {
      callback()
    } else {
      callback(new Error('请输入正确的' + rule.name))
    }
  },
  decimal: (rule, value, callback) => {
    const precision = rule.precision
    const maxlength = rule.maxlength
    if (value && isNaN(value)) {
      (new Error('只能填写数字'))
    }
    if ((value + '').replace('.', '').length > maxlength) {
      (new Error('长度最大' + maxlength + '位'))
    }
    var RegularExp_tmp = '/^[+,-]{0,1}[0-9]{0,' + (maxlength - precision) + '}[.]{1}[0-9]{0,' + precision + '}$|^[+,-]{0,1}[0-9]{0,' + (maxlength - precision) + '}$/'
    // eslint-disable-next-line no-eval
    var RegularExp = eval(RegularExp_tmp)
    if (!RegularExp.test(value)) {
      callback(new Error('格式不正确！请输入整数位' + (maxlength - precision) + '位，小数位' + precision + '位的数字！'))
    }
    callback()
  }
}
export const validateIdCard = (id, backInfo) => {
  let info = {
      y: '1900',
      m: '01',
      d: '01',
      sex: 'male',
      valid: false,
      length: 0
    },
    initDate = function(length) {
      info.length = length
      const a = length === 15 ? 0 : 2
      let temp
      info.y = (a ? '' : '19') + id.substring(6, 8 + a)
      info.m = id.substring(8 + a, 10 + a)
      info.d = id.substring(10 + a, 12 + a)
      info.sex = id.substring(14, 15 + a) % 2 === 0 ? '1' : '0' // 0男 1女
      temp = new Date(info.y, info.m - 1, info.d)
      return (temp.getFullYear() === info.y * 1) &&
        (temp.getMonth() + 1 === info.m * 1) &&
        (temp.getDate() === info.d * 1)
    },
    back = function() {
      return backInfo ? info : info.valid
    }
  if (typeof id !== 'string') return back()
  // 18
  if (/^\d{17}[0-9x]$/i.test(id)) {
    if (!initDate(18)) return back()
    id = id.toLowerCase().split('')
    let wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
      y = '10x98765432'.split(''),
      sum = 0
    for (let i = 0; i < 17; i++) sum += wi[i] * id[i]
    if (y[sum % 11] === id.pop().toLowerCase()) info.valid = true
    return back()
  }
  // 15
  else if (/^\d{15}$/.test(id)) {
    if (initDate(15)) info.valid = true
    return back()
  } else {
    return back()
  }
}
